package com.hamster.crmpre.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hamster.crmpre.config.LoginInterceptor;
import com.hamster.crmpre.mapper.CourseTaskMapper;
import com.hamster.crmpre.mapper.CustomerCourseTaskMapper;
import com.hamster.crmpre.mapper.CustomerMapper;
import com.hamster.crmpre.pojo.CourseTask;
import com.hamster.crmpre.pojo.Customer;
import com.hamster.crmpre.pojo.CustomerCourseTask;
import com.hamster.crmpre.pojo.UserInfo;
import com.hamster.crmpre.service.CourseTaskService;
import com.hamster.crmpre.service.PhoneUserService;
import com.hamster.crmpre.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 参课率的 Service
 * @author Tarry
 * @create 2019/12/30 17:39
 */
@Service
public class CourseTaskServiceImpl implements CourseTaskService {
    @Autowired
    private CourseTaskMapper courseTaskMapper;
    @Autowired
    private CustomerCourseTaskMapper customerCourseTaskMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private PhoneUserService phoneUserService;

    @Override
    public CustomerCourseTask getDetail(String customerId, String courseTaskId) {
        QueryWrapper<CustomerCourseTask> customerCourseTaskQueryWrapper = new QueryWrapper<>();
        customerCourseTaskQueryWrapper.eq("customer_id",customerId).eq("course_task_id",courseTaskId);
        CustomerCourseTask customerCourseTask = customerCourseTaskMapper.selectOne(customerCourseTaskQueryWrapper);
        //如果为空则从获取参课任务，并插入
        if (customerCourseTask == null){
            UserInfo loginUser = LoginInterceptor.getLoginUser();
            Customer customer = customerMapper.selectById(customerId);
            CourseTask courseTask = courseTaskMapper.selectById(courseTaskId);
            CustomerCourseTask newTask = new CustomerCourseTask();
            List<HashMap<String, String>> list = new ArrayList<>();
            List<String> stringList = JsonUtils.parseList(courseTask.getCourse(), String.class);
            for (String s : stringList) {
                HashMap<String, String> map = new HashMap<>();
                map.put("title",s);
                map.put("state","0");
                list.add(map);
            }

            newTask.setContent(JsonUtils.serialize(list));//设置内容
            newTask.setCourseTaskId(courseTaskId);
            newTask.setCourseTaskTitle(courseTask.getTitle());//任务标题
            newTask.setCreateTime(courseTask.getCreateTime());//创建时间
            newTask.setCustomerId(customerId);
            newTask.setCustomerName(customer.getName());
            newTask.setCustomerPhone(customer.getPhone());
            newTask.setDel("0");//删除标记  正常
            newTask.setParticipant(loginUser.getUserId());//参与员工id
            //根据手机号，查询该登录用户的姓名
            String userName = phoneUserService.queryNameByPhone(loginUser.getPhone());
            newTask.setParticipantName(userName);//参与员工姓名
            newTask.setSelectivity(0.0);//参课率
            newTask.setStartTime(courseTask.getStartTime());//开始时间
            newTask.setEndTime(courseTask.getEndTime());//结束时间
            //判断是否达到结束时间
            if (courseTask.getEndTime().getTime()<= new Date().getTime()){
                newTask.setState("1");  //0开始    1结束
            }
            newTask.setState("0");
            customerCourseTaskMapper.insert(newTask);
            return customerCourseTaskMapper.selectById(newTask.getId());//返回新增的记录
        }
        return customerCourseTask;//返回查到的记录
    }

    /**
     * 查询参课率的列表
     * @param courseTaskId
     * @return
     */
    @Override
    public List<CustomerCourseTask> listCustomerCourse(String courseTaskId) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("principal_vip_id",LoginInterceptor.getLoginUser().getUserId());
        List<Customer> customers = customerMapper.selectList(queryWrapper);
        List<CustomerCourseTask> courseTaskList = new ArrayList<>();
        for (Customer customer : customers) {
            courseTaskList.add(this.getDetail(customer.getId(),courseTaskId)) ;
        }

        return courseTaskList;
    }

    /**
     * 提交参课率
     * @param customerCourseTask
     */
    @Override
    public void insertCustomerTask(CustomerCourseTask customerCourseTask) {
        customerCourseTaskMapper.updateById(customerCourseTask);
    }

    /**
     * 查询参课任务 列表
     * @return
     */
    @Override
    public List<CourseTask> listCourseTask() {
        QueryWrapper<CourseTask> courseTaskQueryWrapper = new QueryWrapper<>();
        courseTaskQueryWrapper.eq("del","0").eq("state","1");
        courseTaskQueryWrapper.orderByDesc("id");
        return courseTaskMapper.selectList(courseTaskQueryWrapper);
    }
}
